Imports Cdm.DataCore.UserSecurity
Partial Class MasterPageWithTables
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If UserAuthorization.CurrentUser Is Nothing Then
            'test wfy
            Response.Redirect("~/login.aspx")
        ElseIf UserAuthorization.CurrentUser.PasswordExpires < System.DateTime.Now Then
            Response.Redirect("~/changepassword.aspx")
        End If
        CheckExpire()

    End Sub
    ''' <summary>
    ''' Check if either the session expires, or the last page load was more than the inactivity period.
    ''' If either is the case, redirect the user to the login page.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CheckExpire()
        If Session("lastpageload") IsNot Nothing Then
            Dim lastPageLoadDate As DateTime = Session("lastpageload")
            If DateDiff(DateInterval.Minute, lastPageLoadDate, System.DateTime.Now) > EntityController.GetInactivePeriod Then
                Session("lastpageload") = Nothing
                Session("CurrentUser") = Nothing
                Response.Redirect("~/login.aspx")
            End If
        End If

        Session("lastpageload") = System.DateTime.Now()
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then

            Me.Page.ClientScript.RegisterClientScriptInclude("AssessmentScripts", "AssessmentScripts.js")

            Dim roleId As Integer = UserAuthorization.CurrentUser.RoleId
            Dim itemList As System.Collections.Generic.List(Of Cdm.DataCore.Navigation.ToolbarItem) = Cdm.DataCore.Navigation.ToolbarItem.ListByRole(roleId)

            'Sys Admin user account
            If Not IsParticipantUser() Then
                ' Me.PreferencesHyperLink.Visible = False
                Me.NavPref.Visible = False

                'Me.lnkHelp.Visible = False
                Me.NavHelp.Visible = False
                Me.ChangePasswordLink.Visible = False
                Me.LinkHelp.Visible = False
                ' Me.lnkLogout.Visible = False
                Me.LogoutSpan.Visible = False
                Me.LinkLogout.Visible = False
                Me.NavLogOut.Visible = False
            End If

            If IsParticipantUser() Then
                Me.ParticipantToolbar1.IsSmallToolbar = False
                Me.NavigationImage.ImageUrl = "~/images/navbarblue.png"
                'Me.pnlPage.Height = 98
                Me.pnlPage.Style("top") = "76px"
            Else
                Me.ParticipantToolbar1.IsSmallToolbar = True
                Me.pnlPage.Style("top") = "63px"
            End If
            Me.ParticipantToolbar1.Itemlist = itemList

            'Footer information
            If Application("CCLink") = "true" Then
                lnkCC.Visible = True
            End If

            lblFooter.Text = Application("Footer")
            'Me.ParticipantToolbar1.Itemlist = itemList
            'Me.ParticipantToolbar1.SelectedItem = "Edit"
        End If
    End Sub

    Private Function IsParticipantUser() As Boolean
        Select Case (UserAuthorization.CurrentUser.RoleId)
            Case CInt(Role.RoleType.Participant), CInt(Role.RoleType.Representative), CInt(Role.RoleType.LegalGuardian)
                Return True
            Case Else
                Return False
        End Select
    End Function



End Class

